package com.si.sea_island.utils;

import com.aliyuncs.CommonRequest;
import com.aliyuncs.CommonResponse;
import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.google.gson.Gson;

import java.util.Map;

public class WAlipaySms {
    private static final String SysDomain = "dysmsapi.aliyuncs.com";
    private static final String AccessKeyId_zrjk = "LTAI4FtASRAUypoNG8HsFJmb";
    private static final String AccessKeySecret_zrjk = "wvmCd5WiCQ5nlQoJwcKFmhDM2zXSPN";
    private static final String AccessKeyId_qiyitong = "LTAI4FvoZe4fFRwZsLHUQH4j";
    private static final String AccessKeySecret_qiyitong = "K7ygUSWvXfG7aH2pjVzRE96HleF7og";
    private static final String SignName = "智融金科";
    private static final String SysVersion = "2017-05-25";

//    // 设置鉴权参数，初始化客户端
//    private static DefaultProfile profile = DefaultProfile.getProfile(
//            "cn-hangzhou",// 地域ID
//            AccessKeyId,// 您的AccessKey ID
//            AccessKeySecret);// 您的AccessKey Secret
//    private static IAcsClient client = new DefaultAcsClient(profile);

    private static void log_print(String functionName, Object result) {
        Gson gson = new Gson();
        System.out.println("-------------------------------" + functionName + "-------------------------------");
        System.out.println(gson.toJson(result));
    }

    public static String checkSmsCode(String code) {
        // 短信模板ID
        String sms_code = "SMS_181202256";//通用验证码
        if (code.equals("6f16e84aebea4b8aa69399177b8bf5d7") || code.equals("636ae58fad8642f78dff68fe62abc19f")) {
            sms_code = "SMS_181202256";
        } else {
            sms_code = "SMS_201455772";
        }
        sms_code = "SMS_201455772";
        return sms_code;
    }

    public static CommonRequest checkCommonRequest(String code) {
        CommonRequest request = new CommonRequest();
        request.setSysDomain(SysDomain);
        request.setSysVersion(SysVersion);
        request.setSysAction("SendSms");
        // 设置鉴权参数，初始化客户端
        if (code.equals("6f16e84aebea4b8aa69399177b8bf5d7")
                || code.equals("636ae58fad8642f78dff68fe62abc19f")
                || code.equals("aa30de8e25574886a77fb8a3fe80ff40")) {
            // 短信签名名称。请在控制台签名管理页面签名名称一列查看（必须是已添加、并通过审核的短信签名）。
            request.putQueryParameter("SignName", "智融金科");
        } else {
            request.putQueryParameter("SignName", "企易通");
        }
        return request;
    }

    public static IAcsClient checkIAcsClient(String code) {
        DefaultProfile profile = DefaultProfile.getProfile(
                "cn-hangzhou",// 地域ID
                AccessKeyId_qiyitong,// 您的AccessKey ID
                AccessKeySecret_qiyitong);// 您的AccessKey Secret
        IAcsClient client = new DefaultAcsClient(profile);
        return client;
//        // 设置鉴权参数，初始化客户端
//        if (code.equals("6f16e84aebea4b8aa69399177b8bf5d7") || code.equals("636ae58fad8642f78dff68fe62abc19f")) {
//            DefaultProfile profile = DefaultProfile.getProfile(
//                    "cn-hangzhou",// 地域ID
//                    AccessKeyId_zrjk,// 您的AccessKey ID
//                    AccessKeySecret_zrjk);// 您的AccessKey Secret
//            IAcsClient client = new DefaultAcsClient(profile);
//            return client;
//        } else {
//            DefaultProfile profile = DefaultProfile.getProfile(
//                    "cn-hangzhou",// 地域ID
//                    AccessKeyId_qiyitong,// 您的AccessKey ID
//                    AccessKeySecret_qiyitong);// 您的AccessKey Secret
//            IAcsClient client = new DefaultAcsClient(profile);
//            return client;
//        }
    }

    /**
     * 发送短信
     */
    public static boolean sendSms(String smsType, String phone, String smsCode) throws ClientException {
        CommonRequest request = checkCommonRequest(smsType);
        // 接收短信的手机号码
        request.putQueryParameter("PhoneNumbers", phone);
        // 短信模板ID
        String sms_code = checkSmsCode(smsType);
        request.putQueryParameter("TemplateCode", sms_code);
        // 设置鉴权参数，初始化客户端
        IAcsClient client = checkIAcsClient(smsType);
        // 短信模板变量对应的实际值，JSON格式。
        request.putQueryParameter("TemplateParam", "{\"code\":\""+smsCode+"\"}");
        CommonResponse commonResponse = client.getCommonResponse(request);
        String data = commonResponse.getData();
        String sData = data.replaceAll("'\'", "");
        log_print("sendSms", sData);
        Gson gson = new Gson();
        Map map = gson.fromJson(sData, Map.class);
        Object code_re = map.get("Code");
        return code_re.toString().equals("OK")?true:false;
    }

    public static boolean sendSms2(String smsType, String phone) throws ClientException {
        CommonRequest request = checkCommonRequest(smsType);
        // 接收短信的手机号码
        request.putQueryParameter("PhoneNumbers", phone);
        // 短信模板ID
        String sms_code = "SMS_223193201";
        request.putQueryParameter("TemplateCode", sms_code);
        // 设置鉴权参数，初始化客户端
        IAcsClient client = checkIAcsClient(smsType);
        // 短信模板变量对应的实际值，JSON格式。
        request.putQueryParameter("TemplateParam", "{\"time\":\""+DateUtils.getNowTime()+"\"}");
        CommonResponse commonResponse = client.getCommonResponse(request);
        String data = commonResponse.getData();
        String sData = data.replaceAll("'\'", "");
        log_print("sendSms", sData);
        Gson gson = new Gson();
        Map map = gson.fromJson(sData, Map.class);
        Object code_re = map.get("Code");
        return code_re.toString().equals("OK")?true:false;
    }

    /**
     * 通知用户评估报告完成
     * @param smsType
     * @param phone
     * @return
     * @throws ClientException
     */
    public static boolean sendSms2(String smsType, String phone, String time) throws ClientException {
        CommonRequest request = checkCommonRequest(smsType);
        // 接收短信的手机号码
        request.putQueryParameter("PhoneNumbers", phone);
        // 短信模板ID
        String sms_code = "SMS_223561275";
        request.putQueryParameter("TemplateCode", sms_code);
        // 设置鉴权参数，初始化客户端
        IAcsClient client = checkIAcsClient(smsType);
        // 短信模板变量对应的实际值，JSON格式。
        request.putQueryParameter("TemplateParam", "{\"time\":\""+time+"\"}");
        CommonResponse commonResponse = client.getCommonResponse(request);
        String data = commonResponse.getData();
        String sData = data.replaceAll("'\'", "");
        log_print("sendSms", sData);
        Gson gson = new Gson();
        Map map = gson.fromJson(sData, Map.class);
        Object code_re = map.get("Code");
        return code_re.toString().equals("OK")?true:false;
    }

    public static boolean sendSms3(String smsType, String phone) throws ClientException {
        CommonRequest request = checkCommonRequest(smsType);
        // 接收短信的手机号码
        request.putQueryParameter("PhoneNumbers", phone);
        // 短信模板ID
        String sms_code = "SMS_223561275";
        request.putQueryParameter("TemplateCode", sms_code);
        // 设置鉴权参数，初始化客户端
        IAcsClient client = checkIAcsClient(smsType);
        // 短信模板变量对应的实际值，JSON格式。
        request.putQueryParameter("TemplateParam", "{\"time\":\""+DateUtils.getNowTime()+"\"}");
        CommonResponse commonResponse = client.getCommonResponse(request);
        String data = commonResponse.getData();
        String sData = data.replaceAll("'\'", "");
        log_print("sendSms", sData);
        Gson gson = new Gson();
        Map map = gson.fromJson(sData, Map.class);
        Object code_re = map.get("Code");
        return code_re.toString().equals("OK")?true:false;
    }

    public static boolean sendAssessSeizeOrderSms(String phone) throws ClientException {
        String smsType = "6f16e84aebea4b8aa69399177b8bf5d7";
        CommonRequest request = checkCommonRequest("6f16e84aebea4b8aa69399177b8bf5d7");
        // 接收短信的手机号码
        request.putQueryParameter("PhoneNumbers", phone);
        // 短信模板ID
        String sms_code = "SMS_463775774";
        request.putQueryParameter("TemplateCode", sms_code);
        // 设置鉴权参数，初始化客户端
        IAcsClient client = checkIAcsClient(smsType);
        // 短信模板变量对应的实际值，JSON格式。
        request.putQueryParameter("TemplateParam", "{}");
        CommonResponse commonResponse = client.getCommonResponse(request);
        String data = commonResponse.getData();
        String sData = data.replaceAll("'\'", "");
        log_print("sendSms", sData);
        Gson gson = new Gson();
        Map map = gson.fromJson(sData, Map.class);
        Object code_re = map.get("Code");
        return code_re.toString().equals("OK")?true:false;
    }

    /**
     * 发送短信
     */
//    public static boolean sendSms(CnApp app, String phone, String code, int type) throws ClientException {
//        CommonRequest request = new CommonRequest();
//        request.setSysDomain(SysDomain);
//        request.setSysVersion(SysVersion);
//        request.setSysAction("SendSms");
//        // 接收短信的手机号码
//        request.putQueryParameter("PhoneNumbers", phone);
//        // 短信签名名称。请在控制台签名管理页面签名名称一列查看（必须是已添加、并通过审核的短信签名）。
////        request.putQueryParameter("SignName", SignName);
//        // 短信模板ID
//        String sms_code = "SMS_181202256";//通用验证码
//        // 设置鉴权参数，初始化客户端
//        DefaultProfile profile = DefaultProfile.getProfile(
//                "cn-hangzhou",// 地域ID
//                AccessKeyId,// 您的AccessKey ID
//                AccessKeySecret);// 您的AccessKey Secret
//        IAcsClient client = new DefaultAcsClient(profile);
//        if (app.getAppCode().equals("6f16e84aebea4b8aa69399177b8bf5d7")) {
//            request.putQueryParameter("SignName", "智融金科");
//            sms_code = "SMS_181202256";
//        } else {
//            request.putQueryParameter("SignName", "企易通");
//            sms_code = "SMS_201455772";
//            profile = DefaultProfile.getProfile(
//                    "cn-hangzhou",// 地域ID
//                    "LTAI4FvoZe4fFRwZsLHUQH4j",// 您的AccessKey ID
//                    "K7ygUSWvXfG7aH2pjVzRE96HleF7og");// 您的AccessKey Secret
//            client = new DefaultAcsClient(profile);
//        }
//        request.putQueryParameter("TemplateCode", sms_code);
//        // 短信模板变量对应的实际值，JSON格式。
//        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
//        CommonResponse commonResponse = client.getCommonResponse(request);
//        String data = commonResponse.getData();
//        String sData = data.replaceAll("'\'", "");
//        log_print("sendSms", sData);
//        Gson gson = new Gson();
//        Map map = gson.fromJson(sData, Map.class);
//        Object code_re = map.get("Code");
//        return code_re.toString().equals("OK")?true:false;
//    }

    /**
     * 添加短信模板
     */
//    private String addSmsTemplate() throws ClientException {
//        CommonRequest addSmsTemplateRequest = new CommonRequest();
//        addSmsTemplateRequest.setSysDomain(SysDomain);
//        addSmsTemplateRequest.setSysAction("AddSmsTemplate");
//        addSmsTemplateRequest.setSysVersion(SysVersion);
//        // 短信类型。0：验证码；1：短信通知；2：推广短信；3：国际/港澳台消息
//        addSmsTemplateRequest.putQueryParameter("TemplateType", "0");
//        // 模板名称，长度为1~30个字符
//        addSmsTemplateRequest.putQueryParameter("TemplateName", "测试短信模板");
//        // 模板内容，长度为1~500个字符
//        addSmsTemplateRequest.putQueryParameter("TemplateContent", "您正在申请手机注册，验证码为：${code}，5分钟内有效！");
//        // 短信模板申请说明
//        addSmsTemplateRequest.putQueryParameter("Remark", "测试");
//        CommonResponse addSmsTemplateResponse = client.getCommonResponse(addSmsTemplateRequest);
//        String data = addSmsTemplateResponse.getData();
//        // 消除返回文本中的反转义字符
//        String sData = data.replaceAll("'\'", "");
//        log_print("addSmsTemplate", sData);
//        Gson gson = new Gson();
//        // 将字符串转换为Map类型，取TemplateCode字段值
//        Map map = gson.fromJson(sData, Map.class);
//        Object templateCode = map.get("TemplateCode");
//        return templateCode.toString();
//    }



//    public static String sendSms(String phone,String code,int type) throws ClientException {
//        CommonRequest request = new CommonRequest();
//        request.setSysDomain(SysDomain);
//        request.setSysVersion(SysVersion);
//        request.setSysAction("SendSms");
//        // 接收短信的手机号码
//        request.putQueryParameter("PhoneNumbers", phone);
//        // 短信签名名称。请在控制台签名管理页面签名名称一列查看（必须是已添加、并通过审核的短信签名）。
//        request.putQueryParameter("SignName", SignName);
//        // 短信模板ID
//        String sms_code = "SMS_181202256";//通用验证码
//        if (type == 0) {//注册
//            sms_code = "SMS_181202256";
//        } else if (type == 1) {
//            sms_code = "SMS_181430049";//第三方登录绑定手机号验证码
//        }
//        request.putQueryParameter("TemplateCode", sms_code);
//        // 短信模板变量对应的实际值，JSON格式。
//        request.putQueryParameter("TemplateParam", "{\"code\":\""+code+"\"}");
//        CommonResponse commonResponse = client.getCommonResponse(request);
//        String data = commonResponse.getData();
//        String sData = data.replaceAll("'\'", "");
//        log_print("sendSms", sData);
//        Gson gson = new Gson();
//        Map map = gson.fromJson(sData, Map.class);
//        Object bizId = map.get("BizId");
//        Object code_re = map.get("Code");
//        return bizId.toString();
//    }

    /**
     * 查询发送详情
     */
//    private void querySendDetails(String bizId) throws ClientException {
//        CommonRequest request = new CommonRequest();
//        request.setSysDomain(SysDomain);
//        request.setSysVersion(SysVersion);
//        request.setSysAction("QuerySendDetails");
//        // 接收短信的手机号码
//        request.putQueryParameter("PhoneNumber", "15068727375");
//        // 短信发送日期，支持查询最近30天的记录。格式为yyyyMMdd，例如20191010。
//        request.putQueryParameter("SendDate", "20191226");
//        // 分页记录数量
//        request.putQueryParameter("PageSize", "10");
//        // 分页当前页码
//        request.putQueryParameter("CurrentPage", "1");
//        // 发送回执ID，即发送流水号。
//        request.putQueryParameter("BizId", bizId);
//        CommonResponse response = client.getCommonResponse(request);
//        log_print("querySendDetails", response.getData());
//    }

//    public static void main(String[] args) {
//        AlipaySms sendSmsDemo = new AlipaySms();
//
//        try {
//            // 创建短信模板
//            //String templateCode = sendSmsDemo.addSmsTemplate();
//            // 使用刚创建的短信模板发送短信
//            String bizId = sendSmsDemo.sendSms(0);
//            // 根据短信发送流水号查询短信发送情况
//            sendSmsDemo.querySendDetails(bizId);
//        } catch (ClientException e) {
//            e.printStackTrace();
//        }
//    }
}
